# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=fennel
pkgver=1.5.1
pkgrel=1
pkgdesc="A Lua Lisp Language"
url="https://fennel-lang.org"
arch="all" #detects noarch, but contains compiled binaries
license="MIT"
subpackages="$pkgname-doc $pkgname-lua:_lua_common"
source="fennel-$pkgver.tar.gz::https://git.sr.ht/~technomancy/fennel/archive/$pkgver.tar.gz
	fix_make.patch
	dont_run_git.patch
	remove_math_acos_test.patch"
replaces="fennel"
options="net"

case "$CARCH" in
ppc64le|s390x|riscv64|loongarch64) _lua_versions="5.1 5.2 5.3 5.4" ;;
*) _lua_versions="5.1 5.2 5.3 5.4 jit" ;;
esac

for _v in $_lua_versions; do
	makedepends="$makedepends lua$_v-dev"

	subpackages="$subpackages $pkgname$_v:_subpackage"
done

prepare() {
	default_prepare

	for _v in $_lua_versions; do
		cp -r "$builddir" "$builddir-$_v"
	done
}

build() {
	for _v in $_lua_versions; do
		msg "Building for lua$_v..."
		cd "$builddir-$_v"

		if [ $_v = "5.1" ]; then
			 libdir=/usr/lib/liblua.so
			 includedir=/usr/include
		elif [ $_v = "jit" ]; then
			 libdir=/usr/lib/libluajit-5.1.so
			 includedir=/usr/include/luajit-2.1
		else
			libdir=/usr/lib/lua$_v/liblua.so
			includedir=/usr/include/lua$_v
		fi

		make fennel.lua \
				COMPILE_ARGS="FENNEL_PATH=src/?.fnl FENNEL_MACRO_PATH=src/?.fnl CC_OPTS=" \
				fennel-bin \
				LUA=lua$_v \
				NATIVE_LUA_LIB=$libdir \
				BIN_LUA_VERSION=$_v \
				LUA_INCLUDE_DIR=$includedir \
				BIN_LUA_DIR=/usr/bin/lua$_v

		lua$_v fennel.lua --compile src/fennel/binary.fnl > binary.lua
	done
}

check() {
	for _v in $_lua_versions; do
		msg "Testing for lua$_v..."
		cd "$builddir-$_v"

		make test LUA=lua$_v
	done
}

package() {
	cd "$builddir-5.4"
	gzip -9n man/man1/fennel.1
	gzip -9n man/man3/fennel-api.3
	gzip -9n man/man5/fennel-reference.5
	gzip -9n man/man7/fennel-tutorial.7

	install -Dm644 man/man1/fennel.1.gz \
			-t "$pkgdir"/usr/share/man/man1/
	install -Dm644 man/man3/fennel-api.3.gz \
			-t "$pkgdir"/usr/share/man/man3/
	install -Dm644 man/man5/fennel-reference.5.gz \
			-t "$pkgdir"/usr/share/man/man5/
	install -Dm644 man/man7/fennel-tutorial.7.gz \
			-t "$pkgdir"/usr/share/man/man7/

}

_lua_common() {
	pkgdesc="Fennel Lua Library"
	local dir=$subpkgdir/usr/share/lua/common
	local rockdir=$subpkgdir/usr/lib/luarocks/rocks-common/fennel/$pkgver-1
	mkdir -p "$dir" "$rockdir"
	cp $builddir-5.4/fennel.lua "$dir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}


_subpackage() {
	local _v="${subpkgname#"$pkgname"}"
	pkgdesc="A Lua Lisp Language for Lua $_v"
	install_if="lua$_v $pkgname=$pkgver-r$pkgrel"
	depends="$depends lua$_v"

	cd "$builddir-$_v"

	install -Dm0644 "fennel.lua" \
			-t "$subpkgdir/usr/share/lua/$_v/"

	install -Dm0644 "binary.lua" \
			-t "$subpkgdir/usr/share/lua/$_v/fennel/"

	install -Dm0755 "fennel-bin" \
			"$subpkgdir/usr/bin/fennel$_v"
}

sha512sums="
723f85d62ffc8fc2a958ba52ae491681274ed0b300352cf49067a3456b6f074e74e1540ae27ca102a3e7ccb47e32b3c4dbec6346ae2ede7282903246ef8c5b1d  fennel-1.5.1.tar.gz
f7bc40c9bad2d4780e13c20bf30abfb196d71b57d4404f446395f8df6af58125f0accd97f23e986b0775ac89a85a68e7a98ff9ccd00ac0c8db322517fa11fb15  fix_make.patch
ce8a26e5bc225ff23bba786bbac7b6d26dbdcea03ac9933ebce0baacab374ed99b8919031e571fc851e83f3c963d6218351f91da289c3ea48ba8282b302bc72a  dont_run_git.patch
6435ca670e666b8a114439b4958e887e61a33617d6997c4e8df06a69b0c70744d2b84df6260f37074aff1a27cde1a24a7f19f5ed3fb73304068bfd86a831a6bf  remove_math_acos_test.patch
"
